﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Discovery;
using System.ServiceModel.Description;
using System.Net;

namespace DataFlock.CRM.Engine.Common
{
    internal class Security
    {
        private static ClientCredentials _credentials;

        public static ClientCredentials Credentials
        {
            get
            {
                return _credentials;
            }
        }

        // TODO: sdsdfsd
        public static ClientCredentials DeviceCredentials
        {
            get
            {
                return null;
            }
        }

        internal static AuthenticationProviderType GetServerType(Uri uri)
        {
            return ServiceConfigurationFactory.CreateConfiguration<IDiscoveryService>(uri).AuthenticationType;
        }

        internal static ClientCredentials InitializeLogonCredentials(string username, string password, string domain, bool useDefaultNetworkCredentials)
        {
            _credentials = new ClientCredentials();

            if (useDefaultNetworkCredentials)
            {
                _credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
            }
            else
            {
                AuthenticationProviderType authenticationProviderType = GetServerType(Configuration.DiscoveryServiceUrl);
                
                if (authenticationProviderType == AuthenticationProviderType.ActiveDirectory)
                {
                    _credentials.Windows.ClientCredential = new NetworkCredential(username, password, domain);
                }
                else if (authenticationProviderType == AuthenticationProviderType.LiveId)
                {
                    _credentials.UserName.UserName = username;
                    _credentials.UserName.Password = password;
                }
            }

            return _credentials;
        }

        public static void SetCredentials(string username, string password)
        {
            SetCredentials(username, password, string.Empty);
        }

        public static void SetCredentials(string username, string password, string domain)
        {
            bool useDefaultNetworkCredentials = false;

            if (Configuration.ImpersonateUser)
            {
                username = Configuration.UserName;
                password = Configuration.Password;
                domain = Configuration.Domain;
            }
            else if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
            {
                useDefaultNetworkCredentials = true;
            }

            Security.InitializeLogonCredentials(username, password, domain, useDefaultNetworkCredentials);
        }

        public ClientCredentials GetDeviceCredentials()
        {
            throw new NotImplementedException();
        }
    }
}
